﻿Public Class frm_login

    Private Sub frm_login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Call save_Setting() '@@@@ nanti di hapus, ini hanya sementara

        '# load setting aplikasi
        Call load_Setting()

        '# cek aplikasi apakah baru pertama kali dijalankan, kalau iya buka option.exe
        If APP.Info.Pertamax <> "0" Then ' jika baru pertama kali
            Shell(My.Application.Info.DirectoryPath & "\Pengaturan.exe", AppWinStyle.NormalFocus)
            End
        End If

        '# cek serial
        Dim nSerial As String = GenerateKey(APP.Info.KD_Sekolah)

        If nSerial <> APP.Info.Serial Then
            MsgBox("Serial Nomor tidak sesuai dengan kode sekolah", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Cek Serial")
            End
        End If

        '# cek update aplikasi

        '# cek regional
        If ValidRegional() = False Then
            End
        End If

        '# Init data Sekolah
        '--------------------------------------------------------
        If APP.Info.Jenjang = "SMP" Then
            Call init_List_SMP()
        ElseIf APP.Info.Jenjang = "SMA" Then
            Call init_List_SMA()
        ElseIf APP.Info.Jenjang = "SMK" Then
            Call init_List_SMK()
        End If
        
    End Sub

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        Try
            ' create query
            DB.FlushCache()
            DB.Selects("*")
            DB.From("tbl_pengguna")
            DB.Where("id_pengguna", txt_username.Text)
            DB.Where("kata_sandi", Encript.GetMD5Hash(txt_password.Text))

            ' execute to reader
            Connection.ExecuteToDataReader(DB.GetQueryString)

            ' jika ada row
            If Connection.Read.HasRows Then
                'frm_main.Dispose()
                'frm_main.Show()
                frm_main.Dispose()
                frm_main.Show()

                Me.Close()
            Else
                MsgBox("Login Gagal!" & vbCrLf & "Pastikan Nama Pengguna dan Kata Sandi diisi dengan benar...", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Pesan")
            End If
        Catch ex As Exception
            
        End Try
    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        'Connection.ExecuteNonQuery("TRUNCATE TABLE `tbl_pendaftaran`")
        'BackupDatabase("")
        RestoreDatabase("C:\filesql.sql")
        End
    End Sub

End Class
